package org.database.condition;

import com.mybatisflex.core.datasource.DataSourceManager;
import lombok.extern.slf4j.Slf4j;
import org.database.config.DateBaseStrategyConfiguration;
import org.springframework.context.annotation.Condition;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.core.type.AnnotatedTypeMetadata;
import org.springframework.stereotype.Component;

/**
 * DateBaseStrategyConditional
 *
 * @author grl
 * @date 2024/1/5
 */
@Slf4j
@Component
public class DateBaseConditional implements Condition {

    private static final String enableReadAndWrite = "database.readAndWrite.enable";

    @Override
    public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
        String property = context.getEnvironment().getProperty(enableReadAndWrite);
        if(Boolean.parseBoolean(property)){
            DataSourceManager.setDataSourceShardingStrategy(new DateBaseStrategyConfiguration());
            return true;
        }
        return false;
    }
}
